﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Data;

namespace ProductManagement.Controls
{
    public class Product
    {
        public string ID { get; set; }
        public string Name { get; set; }
        public int Quantity { get; set; }
        public string UnitType { get; set; }
        public string Expire_Date { get; set; }
    }

    public class Unit
    {
        public string Type { get; set; }
        public string Name { get; set; }
    }

    public class TypeProduct
    {
        public string Type { get; set; }
        public string Name { get; set; }
    }

    public partial class ProductDetail : System.Web.UI.UserControl
    {
        public string CurrentId = "";
        public bool update = false;

        protected int _productId;

        public int ProductId
        {
            get { return _productId; }
            set
            {
                _productId = value;
                hidProductId.Value = _productId.ToString();
                SetControlData();
            }
        }


        //protected void Page_Load(object sender, EventArgs e)
        //{
        //    if (!IsPostBack)
        //    {
        //        LoadTypeProduct();
        //        LoadUnitType();
        //        //LoadProductDetail(0);
        //    }
        //}

        //public void LoadProductDetail(int Id)
        //{

        //}

        //public void LoadTypeProduct()
        //{
        //    XmlDocument vidDocument = new XmlDocument();
        //    vidDocument.Load(Server.MapPath("~/ProductList.xml"));
        //    //XmlNodeList products = vidDocument.SelectNodes("/PRODUCTTYPES/PRODUCTTYPE");
        //    XmlNodeList typenames = vidDocument.SelectNodes("/Root/PRODUCTTYPES/PRODUCTTYPE");

        //    foreach (XmlNode node in typenames)
        //    {

        //        rdbTypeProduct.Items.Add(new ListItem(node.Attributes["NAME"].Value,node.Attributes["TYPE"].Value));
        //    }


        //}

        //public void LoadUnitType()
        //{
        //    XmlDocument vidDocument = new XmlDocument();
        //    vidDocument.Load(Server.MapPath("~/ProductList.xml"));
        //    //XmlNodeList products = vidDocument.SelectNodes("/PRODUCTTYPES/PRODUCTTYPE");
        //    XmlNodeList typenames = vidDocument.SelectNodes("/Root/UNITS/UNIT");

        //    foreach (XmlNode node in typenames)
        //    {

        //        ddlUnit.Items.Add(new ListItem(node.Attributes["NAME"].Value, node.Attributes["TYPE"].Value));
        //    }
        //    ddlUnit.DataBind();
        //}

        public bool IsExistProductID(string id)
        {
            bool isExist = false;
            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath("~/App_Data/ProductManager.xml"));
            XmlNodeList nodeList;
            XmlNode root = doc.DocumentElement;
            nodeList = root.SelectNodes("descendant::product[ID='" + int.Parse(txtIDProduct.Text).ToString() + "']");

            if (nodeList.Count==1)
            {
                return true;
            }

            return false;
        }

        public void CreateNewID()
        {
            var manager = new ProductManager();
            var dt = manager.GetProductList(0, 0);
            if (dt != null)
            {
                int max = int.Parse(dt.Compute("Max(ID)", "").ToString());
                if (_productId == 0)
                    _productId = max + 1;


            }
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            CreateNewID();
            txtIDProduct.Text = _productId.ToString("000000");
            //radProductType.SelectedIndex = 1;
            txtQuantity.Text = "0";
            txtExpireDate.Text = DateTime.Now.ToShortDateString();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            
            //Check ton tai ProductID
            if (IsExistProductID(txtIDProduct.Text))
            {

                SaveProduct(true);
                //Add vo database

            }
            else
            {
                SaveProduct(false);

            }
        }

        public void SaveProduct(bool status)
        {
            //Status=true : Update
            //       false : Insert
            
            if (status)
            {
                //Update
            }
            else
            {
                //Insert
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {

        }






        protected void SetControlData()
        {
            var manager = new ProductManager();
            var dt = manager.GetSelectedProduct(ProductId);
            //txtProductId.Text = CStr(dt.Rows(0)("PRODUCT_ID"))
            //txtProductName.Text = CStr(dt.Rows(0)("PRODUCT_NAME"))
            if (dt != null)
            {
                txtIDProduct.Text = dt.Rows[0]["ID"].ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                 LoadRadProductType();
                 LoadCmbUnit();                
            }
        }

        protected void LoadRadProductType()
        {
            var manager = new ProductManager();
            var dtProductType = manager.GetProductType();  
          
            radProductType.DataSource = dtProductType;
            radProductType.DataTextField = "NAME";
            radProductType.DataValueField = "ID";
            radProductType.SelectedIndex = 1;
            radProductType.DataBind();

        }

        protected void LoadCmbUnit()
        {
            var manager = new ProductManager();
            var dtUnitType = manager.GetUnitType();

            ddlUnit.DataSource = dtUnitType;
            ddlUnit.DataTextField = "NAME";
            ddlUnit.DataValueField = "ID";
            ddlUnit.DataBind();
        }


        
    }
}